﻿using System;
using System.Threading.Tasks;
using PortalWeb.Filter;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity.Owin;
using PortalWeb.Core.Entity;
using Quick.Common.Logging;
using System.Collections.Generic;
using System.ComponentModel;
using PortalWeb.Common.UI;
using PortalWeb.Core.IManager;

namespace PortalWeb.Areas.Admin.Controllers
{

    [Navbar("权限管理", "系统管理")]
    [CustomAuthorize]
    public class AuthorizeController : Controller
    {
        private IRoleGroupManager _roleGroupManager;
        private IFunctionManager _systemFunctionManager;

        public AuthorizeController(IRoleGroupManager roleManager, IFunctionManager systemFunctionManager)
        {
            _roleGroupManager = roleManager;
            _systemFunctionManager = systemFunctionManager;
        }
        public IFunctionManager SystemFunctionManager
        {
            get
            {
                return _systemFunctionManager ?? HttpContext.GetOwinContext().GetUserManager<IFunctionManager>();
            }
            private set { _systemFunctionManager = value; }
        }
        public IRoleGroupManager RoleGroupManager
        {
            get { return _roleGroupManager ?? HttpContext.GetOwinContext().GetUserManager<IRoleGroupManager>(); }
            private set { _roleGroupManager = value; }
        }

        // GET: Admin/Authorize
        public ActionResult Index()
        {
            return View();
        }

        [CustomAuthorize]
        public async Task<JsonResult> RoleTree(string sort = "Id", string order = "")
        {
            try
            {
                List<object> list = new List<object>();
                var model = await RoleGroupManager.LoadAllEntitiesnAsync();
                foreach (var item in model)
                {
                    var nodes = new List<object>();
                    foreach (var item2 in item.Children)
                    {
                        nodes.Add(new { icon = "fa fa-folder", text = item2.Name });
                    }
                    foreach (var item3 in item.Roles)
                    {
                        nodes.Add(new { icon = "fa fa-group", text = item3.Name });
                    }
                    list.Add(new { icon = "fa fa-folder", text = item.Name, nodes = nodes });
                }

                return Json(list, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                FileLogUtility.ErrorFormat("RoleTree", ex);
                return Json(new { error = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [CustomAuthorize]
        public async Task<JsonResult> FunctionTree(int? id, string sort = "Id", string order = "")
        {
            try
            {
                var model = await SystemFunctionManager.GetByParentIdAsync(id);
                return Json(new { status = true, data = ToTreeList(model) }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                FileLogUtility.ErrorFormat("RoleTree", ex);
                return Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        private List<object> ToTreeList(IEnumerable<Function> model)
        {
            List<object> list = new List<object>();
            foreach (var item in model)
            {
                List<object> nodes = null;
                if (item.Children != null && item.Children.Count > 0)
                {
                    nodes = ToTreeList(item.Children);
                }
                list.Add(new
                {
                    pid =item.Id,
                    text = item.Name,
                    icon = item.Icon,
                    nodes = nodes,
                    tags = new string[] { item.Type.GetDescription(), item.ActionUrl }
                });

            }
            return list;
        }
    }
}